﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Common
{
    public class ConvertHelper
    {
        // Methods
        public static string ConvertDataTableToXml(DataTable dt)
        {
            string str = string.Empty;
            if (dt != null)
            {
                MemoryStream w = null;
                XmlTextWriter writer = null;
                try
                {
                    w = new MemoryStream();
                    writer = new XmlTextWriter(w, Encoding.Unicode);
                    dt.WriteXml(writer);
                    int length = (int)w.Length;
                    byte[] buffer = new byte[length];
                    w.Seek(0L, SeekOrigin.Begin);
                    w.Read(buffer, 0, length);
                    UnicodeEncoding encoding = new UnicodeEncoding();
                    str = encoding.GetString(buffer).Trim();
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        w.Close();
                        w.Dispose();
                    }
                }
            }
            return str;
        }

        private static decimal ConvertStringToDecimal(string s)
        {
            decimal num;
            decimal.TryParse(s, out num);
            return num;
        }

        private static double ConvertStringToDouble(string s)
        {
            double num;
            double.TryParse(s, out num);
            return num;
        }

        private static int ConvertStringToInteger(string s)
        {
            int num;
            int.TryParse(s, out num);
            return num;
        }

        private static long ConvertStringToLong(string s)
        {
            long num;
            long.TryParse(s, out num);
            return num;
        }

        private static short ConvertStringToShortInt(string s)
        {
            short num;
            short.TryParse(s, out num);
            return num;
        }

        public static DataSet ConvertXmlToDataSet(string xmlStr)
        {
            if (!string.IsNullOrEmpty(xmlStr))
            {
                StringReader input = null;
                XmlTextReader reader = null;
                try
                {
                    DataSet set = new DataSet();
                    input = new StringReader(xmlStr);
                    reader = new XmlTextReader(input);
                    set.ReadXml(reader);
                    return set;
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                        input.Close();
                        input.Dispose();
                    }
                }
            }
            return null;
        }

        public static DataTable ConvertXmlToDataTable(string xmlStr)
        {
            return ConvertXmlToDataTable(xmlStr, 0);
        }

        public static DataTable ConvertXmlToDataTable(string xmlStr, int tableIndex)
        {
            DataSet set = ConvertXmlToDataSet(xmlStr);
            if ((set != null) && (set.Tables.Count > tableIndex))
            {
                return set.Tables[tableIndex];
            }
            return null;
        }

        public static bool GetBoolean(object obj)
        {
            if ((obj == DBNull.Value) || (obj == null))
            {
                return false;
            }
            if (!(GetString(obj).ToLower() == "true"))
            {
                return false;
            }
            return true;
        }

        public static DateTime GetDateTime(object obj)
        {
            DateTime time;
            DateTime.TryParse(GetString(obj), out time);
            return time;
        }

        public static string GetDateTimeString(object obj, string sFormat)
        {
            return GetDateTime(obj).ToString(sFormat);
        }

        public static decimal GetDecimal(object obj)
        {
            return ConvertStringToDecimal(GetString(obj));
        }

        public static double GetDouble(object obj)
        {
            return ConvertStringToDouble(GetString(obj));
        }

        public static int GetInteger(object obj)
        {
            return ConvertStringToInteger(GetString(obj));
        }

        public static long GetLong(object obj)
        {
            return ConvertStringToLong(GetString(obj));
        }

        public static string GetShortDateString(object obj)
        {
            return GetDateTimeString(obj, "yyyy-MM-dd");
        }

        public static short GetShortInt(object obj)
        {
            return ConvertStringToShortInt(GetString(obj));
        }

        public static string GetString(object obj)
        {
            if ((obj != DBNull.Value) && (obj != null))
            {
                return obj.ToString();
            }
            return "";
        }
    }

}
